﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Drawing;
//using System.ServiceModel;
//using System.Runtime.Serialization;

namespace BattleshipContract
{
    [Serializable]
    public class Ship
    {
        //private int length;
        //private Point position;

        private string shipType, shipPosition;
        private int shipHealth, xCoordinate, yCoordinate, shipHeight, shipWidth;

        public int YCoordinate
        {
            get { return yCoordinate; }
            set { yCoordinate = value; }
        }

        public int XCoordinate
        {
            get { return xCoordinate; }
            set { xCoordinate = value; }
        }

        public Ship(string type, string position, int row, int column)
        {
            this.shipType = type;
            this.shipPosition = position;
            xCoordinate = column;
            yCoordinate = row;
            this.setHealth();
        }

        public Ship Clone()
        {
            return new Ship(shipType, shipPosition, xCoordinate, yCoordinate);
        }

        /// <summary>
        /// type of ships:BATTLESHIP,DESTROYER,SUBMARINE,CARRIER SHIP,PATROL BOAT
        /// </summary>
        public string ShipType
        {
            get { return shipType; }
            set { shipType = value; }
        }

        /// <summary>
        /// VERTICAL or HORIZONTAL, determine occupied tiles
        /// </summary>
        public string ShipPosition
        {
            get { return shipPosition; }
            set { shipPosition = value; }
        }

        /// <summary>
        /// determine ship's remaining health (from 5 to 0) and number of occupied tiles
        /// </summary>
        public int ShipHealth
        {
            get { return shipHealth; }
            set { shipHealth = value; }
        }

        /// <summary>
        /// record all occupied tiles by a ship, calculated with starting coordinate, position, and max health
        /// </summary>
        /// <returns>true if tile is not free</returns>
        public bool isOccupied(int x, int y)
        {
            bool value = false;
            int tempX = 0, tempY = 0;

            for (int d = 0; d < 10; d++)
            {
                if (x > 28 * d)
                    tempX = (28 * d) + 1;
                if (y > 28 * d)
                    tempY = (28 * d) + 1;
            }

            if (shipPosition == "VERTICAL")
            {
                if (tempX == xCoordinate && tempY >= yCoordinate && tempY <= (yCoordinate + (28 * shipHealth)))
                    value = true;
            }
            else if (shipPosition == "HORIZONTAL")
            {
                if (tempY == yCoordinate && tempX >= xCoordinate && tempX <= (xCoordinate + (28 * shipHealth)))
                    value = true;
            }
            return value;
        }


        /// <summary>
        /// record new array occupied tiles when the ship is moved
        /// </summary>
        public void changeCoordinate(int row, int col)
        {
            int tempX = 0, tempY = 0;
            for (int d = 0; d < 10; d++)
            {
                if (row > 28 * d)
                    tempY = (28 * d) + 1;
                if (col > 28 * d)
                    tempX = (28 * d) + 1;
            }

            xCoordinate = tempX; yCoordinate = tempY;

            Console.WriteLine("xCoordinate: " + xCoordinate.ToString() + ", yCoordinate: " + yCoordinate.ToString());
        }

        /// <summary>
        /// define the health depending on ship's type
        /// </summary>
        public void setHealth()
        {
            if (shipType == "BATTLESHIP")
            {
                shipHealth = 5;
                shipHeight = 26;
                shipWidth = 141;
            }
            else if (shipType == "DESTROYER")
            {
                shipHealth = 4;
                shipHeight = 26;
                shipWidth = 115;
            }
            else if (shipType == "SUBMARINE")
            {
                shipHealth = 3;
                shipHeight = 26;
                shipWidth = 87;
            }
            else if (shipType == "CARRIER SHIP")
            {
                shipHealth = 2;
                shipHeight = 26;
                shipWidth = 55;
            }
            else if (shipType == "PATROL BOAT")
            {
                shipHealth = 2;
                shipHeight = 26;
                shipWidth = 55;
            }
        }

        public void draw(System.Windows.Forms.PaintEventArgs paint)
        {
            if (shipType == "BATTLESHIP")
            {
                paint.Graphics.DrawImage(System.Drawing.Image.FromFile(@"spirit_of_viking\battleship-vertical.png", false), xCoordinate, yCoordinate, shipHeight, shipWidth);
            }
            else if (shipType == "DESTROYER")
            {
                paint.Graphics.DrawImage(System.Drawing.Image.FromFile(@"spirit_of_viking\elvendestroyer-vertical.png", false), xCoordinate, yCoordinate, shipHeight, shipWidth);
            }
            else if (shipType == "SUBMARINE")
            {
                paint.Graphics.DrawImage(System.Drawing.Image.FromFile(@"spirit_of_viking\gnomishsubmarine-vertical.png", false), xCoordinate, yCoordinate, shipHeight, shipWidth);
            }
            else if (shipType == "CARRIER SHIP")
            {
                paint.Graphics.DrawImage(System.Drawing.Image.FromFile(@"spirit_of_viking\humantransport-vertical.png", false), xCoordinate, yCoordinate, shipHeight, shipWidth);
            }
            else if (shipType == "PATROL BOAT")
            {
                paint.Graphics.DrawImage(System.Drawing.Image.FromFile(@"spirit_of_viking\humanoiltanker-vertical.png", false), xCoordinate, yCoordinate, shipHeight, shipWidth);
            }
        }

        //public void drawShip()
        //{}

        //public bool isCellFree(Point shipPos)
        //{ return true; }

        //public void killShip()
        //{}

    }
}
